package com.wanmait.computershop.webController;

import com.wanmait.computershop.dao.CartDAO;
import com.wanmait.computershop.dao.ProductDAO;
import com.wanmait.computershop.dao.WebFavoritesDao;
import com.wanmait.computershop.vo.Product;
import com.wanmait.computershop.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("web")
public class WebFavoritesController {
    @GetMapping("favorites")
    public String favorites(String uid, Model model){
        if (uid.equals("")){
            return "web/login";
        }else {
            WebFavoritesDao webFavoritesDao = new WebFavoritesDao();
            List<Product> favorProductList =  webFavoritesDao.findFavorByID(Integer.parseInt(uid));
            model.addAttribute("favorProductList",favorProductList);
            return "web/product/favorites";         
        }       
    }
    @GetMapping("addFavor")
    @ResponseBody
    public void addFavor(Integer pid,HttpSession session){
        //获取用户信息
        User user = (User) session.getAttribute("loginUser");
        
        System.out.println(pid);
        
        WebFavoritesDao webFavoritesDao = new WebFavoritesDao();
        webFavoritesDao.addFavorByID(user.getId(),pid);
        
        //更新session
        List<Product> productList = webFavoritesDao.findFavorByID(user.getId());
        session.setAttribute("userFavorList", productList);
    }
    @GetMapping("delFavor")
    @ResponseBody
    public void delFavor(Integer pid,HttpSession session){
        //获取用户信息
        User user = (User) session.getAttribute("loginUser");

        System.out.println(pid);

        WebFavoritesDao webFavoritesDao = new WebFavoritesDao();
        webFavoritesDao.delFavorByID(user.getId(),pid);

        //更新session
        List<Product> productList = webFavoritesDao.findFavorByID(user.getId());
        session.setAttribute("userFavorList", productList);
    }
}
